import 'package:homepad/iot/basic_types.dart';
import 'package:homepad/iot/air_conditioner.dart';
import 'package:homepad/iot/fan.dart';
import 'package:homepad/iot/humidity_sensor.dart';
import 'package:homepad/iot/light_onoff.dart';
import 'package:homepad/iot/light_color.dart';
import 'package:homepad/iot/basic_node.dart';
import 'package:homepad/iot/temperature_sensor.dart';

class NovaConfig {
  List<BasicNode> nodes = [];
  String remoteUrl = "";
  String location = "";
  String home = "";
  List<EndpointId> _primaryNodeIds = [];
  List<EndpointId> _secondaryNodeIds = [];

  NovaConfig();

  BasicNode? getNode(EndpointId endpoint) {
    for (var value in nodes) {
      if (value.endpointId == endpoint) {
        return value;
      }
    }
    return null;
  }

  BasicNode? getNodeByType(DeviceType devType) {
    for (var value in nodes) {
      if (value.devType == devType) {
        return value;
      }
    }
    return null;
  }

  BasicNode? getChildNode(EndpointId parent, DeviceType devType) {
    for (var item in nodes) {
      if (item.parentId == parent && item.devType == devType) {
        return item;
      }
    }
    return null;
  }

  BasicNode? getNodeByRoomAndDevType(String room, DeviceType devType) {
    for (var item in nodes) {
      if (item.devType != devType) {
        continue;
      }
      BasicNode? parent;
      if (item.parentId != null) {
        parent = getNode(item.parentId!);
      }
      if (item.room == room || parent?.room == room) {
        return item;
      }
    }
    return null;
  }

  BasicNode? getPrimaryNode(int index) {
    if (index >= _primaryNodeIds.length) {
      return null;
    }
    return getNode(_primaryNodeIds[index]);
  }

  BasicNode? getSecondaryNode(int index) {
    if (index >= _secondaryNodeIds.length) {
      return null;
    }
    return getNode(_secondaryNodeIds[index]);
  }

  static List<BasicNode> parseNodes(dynamic data) {
    return (data as List<dynamic>).map<BasicNode>((item) {
      final json = item as Map<String, dynamic>;
      return switch (DeviceType.byId(json["deviceType"] as int)) {
        DeviceType.loOnOff || DeviceType.loOnOffSwitch => LightOnOff.fromJson(json),
        DeviceType.colorLight => LightColor.fromJson(json),
        DeviceType.thermostat => AirConditioner.fromJson(json),
        DeviceType.fan => Fan.fromJson(json),
        DeviceType.temperatureSensor => TemperatureSensor.fromJson(json),
        DeviceType.humiditySensor => HumiditySensor.fromJson(json),
        _ => BasicNode.fromJson(json)
      };
    }).toList();
  }

  static List<EndpointId> parseEndpointIds(dynamic data) {
    return (data as List<dynamic>).map<EndpointId>((item) {
      return item as EndpointId;
    }).toList();
  }

  NovaConfig.fromJson(Map<String, dynamic> json)
      : nodes = parseNodes(json["nodes"]),
        remoteUrl = json["remote_url"] as String,
        home = json["home"] as String,
        location = json["location"] as String,
        _primaryNodeIds = parseEndpointIds(json["primary_nodes"]),
        _secondaryNodeIds = parseEndpointIds(json["secondary_nodes"]);
}
